package com.xpsoft.oa.service.project.impl;


import java.text.DecimalFormat;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.xpsoft.core.service.impl.BaseServiceImpl;
import com.xpsoft.oa.dao.project.ContractDao;
import com.xpsoft.oa.dao.project.ProjectDao;
import com.xpsoft.oa.model.project.Contract;
import com.xpsoft.oa.model.project.Project;
import com.xpsoft.oa.service.project.ContractService;

public class ContractServiceImpl extends BaseServiceImpl<Contract> implements ContractService{
	private ContractDao dao;
	@Autowired
	private ProjectDao projectDao;
	
	public ContractServiceImpl(ContractDao dao) {
		super(dao);
		this.dao=dao;
	}

	@Override
	public String getNewContractNoByProId(Long proId) {
		Project project = projectDao.get(proId);
		String search = project.getProNo()+"-";
		String newNo="";
		String contractNo = dao.getMaxNo(search);
		if(StringUtils.isBlank(contractNo)){
			newNo = search+ "001z";
		}else{
			DecimalFormat df = new DecimalFormat("000");
			newNo = search + df.format(1 + Integer.parseInt(contractNo.substring(3,contractNo.length()-1)))+"z";
		}
		return newNo;
	}

}